<?php
/*
    [GRIRMS] (C) 2010 Grandison & JM Technologies

    @author kevin<hasson.lai@mobile-grandison.com>
    @version $Id: datatable.func.php 1 2010-09-20 $
*/

	
	function mk_hidden($hidden_array){
		$html = '';
		foreach($hidden_array as $name => $value){
			$html .= "<input type=\"hidden\" id=\"post_$name\" name=\"post_$name\" value=\"$value\" /> ";
		}
		return $html;
	}
	
	function mk_textarea($name, $value='', $width=200, $height=60, $extra=''){
		$style = "width:{$width}px;height:{$height}px;";
		if(preg_match('/(style\=[\'"?])/is', $extra)){
			$extra = preg_replace('/(style\=[\'"?])/is', '\1'.$style, $extra);
			$style = '';
		}else{
			$style = " style=\"$style\"";
		}
		if($extra != '') $extra .= ' '.$extra;
		$html = "<textarea id=\"post_$name\" name=\"post_$name\" value=\"$value\"$style$extra>$value</textarea> ";
		return $html;
	}
	
	function mk_textbox($name, $value='', $width=200, $extra=''){
		$style = "width:{$width}px;";
		if(preg_match('/(style\=[\'"?])/is', $extra)){
			$extra = preg_replace('/(style\=[\'"?])/is', '\1'.$style, $extra);
			$style = '';
		}else{
			$style = " style=\"$style\"";
		}
		if($extra != '') $extra .= ' '.$extra;
		$html = "<input type=\"text\" id=\"post_$name\" name=\"post_$name\" value=\"$value\"$style$extra /> ";
		return $html;
	}
		
	function mk_pwdbox($name, $value='', $width=200, $extra=''){
		$style = "width:{$width}px;";
		if(preg_match('/(style\=[\'"?])/is', $extra, $extra)){
			$extra = preg_replace('/(style\=[\'"?])/is', '\1'.$style);
			$style = '';
		}else{
			$style = " style=\"$style\"";
		}
		if($extra != '') $extra .= ' '.$extra;
		$html = "<input type=\"password\" id=\"post_$name\" name=\"post_$name\" value=\"$value\"$style$extra /> ";
		return $html;
	}
	
	function mk_button($name, $value='', $extra=''){
		$_name = $name == '' ? 'button'.rand(1000,9999) : $name;
		$showname = $name == '' ? '' : 'name="post_'.$_name.'"';
		$html = "<input type=\"button\" id=\"post_$_name\" $showname value=\"$value\" $extra /> ";
		return $html;
	}
	
	function mk_submit($name, $value='', $extra=''){
		$_name = $name == '' ? 'button'.rand(1000,9999) : $name;
		$showname = $name == '' ? '' : 'name="post_'.$_name.'"';
		$html = "<input type=\"submit\" id=\"post_$_name\" $showname value=\"$value\" $extra /> ";
		return $html;
	}
	
	function mk_radiobox($name, $data, $default='', $extra=''){
		$html = '';
		foreach($data as $value => $text){
			$checked = '';
			if(!is_array($default)) $default = $default == '' ? array() : array($default);
			$i = 0;
			foreach($default as $v){
				if($value == $v){
					$checked = ' checked="checked"';
					break;
				}
			}
			$i++;
			$html .= "<label><input type=\"radio\" id=\"post_$name_$i\" name=\"post_{$name}[]\" value=\"$value\"$checked $extra />$text</label> ";
		}
		return $html;
	}
	
	function mk_checkbox($name, $data, $default='', $extra=''){
		$html = '';
		$i = 0;
		foreach($data as $value => $text){
			$checked = '';
			if(!is_array($default)) $default = $default == '' ? array() : array($default);
			foreach($default as $v){
				if($value == $v){
					$checked = ' checked="checked"';
					break;
				}
			}
			$i++;
			$html .= "<label><input type=\"checkbox\" id=\"post_$name_$i\" name=\"post_{$name}[]\" value=\"$value\"$checked $extra />$text</label> ";
		}
		return $html;
	}
	
	function mk_select($name, $data, $default='', $width=0, $extra=''){
		$style = ($width > 0) ? "width:{$width}px;" : '';
		if($style != '' && preg_match('/(style\=[\'"?])/is', $extra)){
			$extra = preg_replace('/(style\=[\'"?])/is', '\1'.$style, $extra);
			$style = '';
		}else{
			$style = " style=\"$style\"";
		}
		if($extra != '') $extra .= ' '.$extra;
		$html = "<select id=\"{$name}\" name=\"{$name}\"{$style}{$extra}>";
		foreach($data as $value => $text){
			$selected = '';
			if(!is_array($default)) $default = $default == '' ? array() : array($default);
			foreach($default as $v){
				if($value == $v){
					$selected = ' selected="selected"';
					break;
				}
			}
			$html .= "<option value=\"$value\"$selected>$text</option> ";
		}
		$html .= "</select> ";
		return $html;
	}
?>
